home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk27 / hopalong / hop.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  6KB  |  220 lines

  1. /*
  2.         First Wave program              Ranjit Bhatnagar 
  3.                                         ranjit@eniac.seas.upenn.edu
  4.  
  5.         All this code is public domain - as if anyone would want
  6.         to steal it.  I would love to see improved versions -
  7.         a nicer interface, a fixed-point implementation for speed.
  8.  
  9. 30 Aug '88
  10.         Shell of First Wave has been
  11.         modified to be a quick Hopalong - as described in Dewdney's
  12.         Mathematical Games column, "Scientific American" Sept 86 -
  13.         and invented by Barry Martin.
  14.  
  15.         Basically, it's an iterated function:
  16.                 x = 0
  17.                 y = 0
  18.                 for i = 1 to num
  19.                         plot (x, y)
  20.                         xx = y - sign(x)*sqrt[abs(b*x-c)]
  21.                         yy = a - x
  22.                         x = xx
  23.                         y = yy
  24.  
  25.         where a, b, and c are constants entered by the user.  (Some suggested
  26.         values: -1000, .1, -10; -200, .1, -80; .4, 1, 0; -3.14, .3, .3)
  27.  
  28.         A simpler function (not implemented here) is
  29.  
  30.                 x = y - sin(x)
  31.                 y = a - x
  32.  
  33.         Martin recommends values of a within .07 of pi for this one.
  34.  
  35.         user must also specify the range of values that will be visible on
  36.         the screen. [INSTEAD I put in "magnification"]
  37.  
  38.         Things to do: 
  39.                 allow specification of visible range.  
  40.                 Clever colors.  
  41.                 Fixed-point for speed.
  42.  
  43.         the latest: changed to interlace, doubled pixel height, reduced
  44.         frequency of checks for close-box...
  45. */
  46.  
  47.  
  48.  
  49. /* create a nice screen */      /* Thanks Rob Peck and "Programmer's Guide" */
  50. #include "hop.h"
  51. struct TextAttr waveScrFont = { /* for basic methods in Intuition */
  52.         "topaz.font", 8, 0, 0
  53. };
  54.  
  55.  
  56. #define WID 640
  57. #define HEI 400
  58. struct NewScreen waveScr = {
  59.         0, 0,                   /* left and top edges */
  60.         WID, HEI,               /* width and height   */
  61.         4,                              /* bitplanes          */
  62.         1, 0,                   /* detail and block pens */
  63.         LACE|HIRES,                     /* screen view type */
  64.         CUSTOMSCREEN,   /* type */
  65.         &waveScrFont,   /* font */
  66.         "Hopalong",     /* title */
  67.         NULL,                   /* user gadgets */
  68.         NULL                    /* custom bitmap */
  69. };
  70.  
  71.  
  72. /* some pretty colors for it */
  73. UWORD waveCols[] = {
  74.         0x0000, 0x0e30, 0x0fff, 0x0b40,
  75.         0x0b04, 0x05d0, 0x0ed0, 0x07df,
  76.         0x069f, 0x0c0e, 0x0f2e, 0x0feb,
  77.         0x0c96, 0x0bbb, 0x07df, 0x0bf0
  78. };
  79.  
  80.  
  81. /* and a nice window */
  82. struct NewWindow waveWin = {
  83.         0, 15,                  /* left and top edges */
  84.         WID-1, HEI-16,  /* size */
  85.         2, 3,                   /* detail and block pens */
  86.         CLOSEWINDOW,    /* flags */
  87.         SIMPLE_REFRESH | WINDOWCLOSE | GIMMEZEROZERO,
  88.         NULL, NULL,
  89.         "Hopalong",
  90.         NULL,                   /* the screen */
  91.         NULL,                   /* the bitmap */
  92.         10, 10, -1, -1, 
  93.         CUSTOMSCREEN
  94. };
  95.  
  96.  
  97. /* the precomputed tables */
  98. /* uncomment these if you decide to put fixedpoint into the program... */
  99. /* extern WORD sinetab[];*/             /* 257 sines from 0 to 2pi, all 16 bits are frac */
  100. /* extern WORD sqrttab[];*/             /* 257 sqrts from 0 to 256, low 12 bits are frac */
  101.  
  102. /* extern double ran(); */
  103.  
  104. /* globals */
  105.  
  106. struct Screen *scr = 0;
  107. struct Window *win = 0;
  108. struct RastPort *rp = 0;
  109. struct ViewPort *vp = 0;
  110.  
  111. int GfxBase = 0;
  112. int IntuitionBase = 0;
  113.  
  114.  
  115.  
  116. /* Open libraries & such */
  117. OpenThings()
  118. {
  119.         GfxBase = OpenLibrary("graphics.library",0);
  120.         if (GfxBase == 0) Die("Couldn't open graphics.library");
  121.         diag("opened grafix");
  122.  
  123.         IntuitionBase = OpenLibrary("intuition.library",0);
  124.         if (IntuitionBase == 0) Die("Couldn't open intuition.library");
  125.         diag("opened intuition");
  126.  
  127.         scr = OpenScreen(&waveScr);
  128.         if (scr == 0) Die("Screen nailed shut");
  129.         diag("opened screen");
  130.  
  131.         vp = &(scr->ViewPort);
  132.  
  133.         waveWin.Screen = scr;
  134.         win = OpenWindow(&waveWin);
  135.         if (win == 0) Die("Window painted shut");
  136.         diag("opened window");
  137.  
  138.         rp = win->RPort;
  139. }
  140.  
  141.  
  142. /* Fail */
  143. Die(s)
  144. char *s;
  145. {
  146.         printf("%s\nSo much for this program.\n",s);
  147.         CloseThings();
  148. }
  149.  
  150.  
  151. /* Close libraries & such */
  152. CloseThings()
  153. {
  154.         diag("Closing everything up.");
  155.         if (win) CloseWindow(win);
  156.         if (scr) CloseScreen(scr);
  157.         if (IntuitionBase) CloseLibrary(IntuitionBase);
  158.         if (GfxBase) CloseLibrary(GfxBase);
  159. }
  160.  
  161.  
  162. /* display a message */
  163. diag(s)
  164. char *s;
  165. {
  166.         printf("%s\n",s);
  167. }
  168.  
  169.  
  170.  
  171. /* THIS IS IT */
  172. main()
  173. {
  174.         struct IntuiMessage *msg;
  175.         int i, j;       /* current pt */
  176.         int m,n;                /* just keep track of how many pixels we've plotted */
  177.         float a, b, c, x, y, xx, yy;
  178.         int foo;        /* sure wish C had a "sign" function and an "abs" */
  179.         float bar;
  180.         float mag;
  181.  
  182.  
  183.         OpenThings();
  184.  
  185.         LoadRGB4(vp, waveCols, 16);
  186.         SetDrMd(rp, JAM1);
  187.  
  188.         n = x = y = 0;
  189.  
  190.         WBenchToFront();
  191.         printf("Please enter values for a, b, c, and magnification: ");
  192.         scanf("%e %e %e %e",&a,&b,&c,&mag);
  193.         c = sin((double)b);     /* force math library to be loaded now */
  194.         WBenchToBack();
  195.  
  196.         while (0 == (msg = (struct IntuiMessage *)GetMsg(win->UserPort))) {
  197.                 for (m=0; m<100; m++) { /* check for messages only every 100th */
  198.                         SetAPen(rp, (++n >> 8) % 16);/* for now, color changes every 256 pts */
  199.                         i = (int)(WID/2+mag*x) % 2000;  /* 2000 picked out of a hat */
  200.                         j = (int)(HEI/2+mag*y) % 2000;
  201.                         WritePixel(rp, i, j);
  202.                         WritePixel(rp, i, j+1); /* why?  To reduce interlace flicker */
  203.                         if (x<0) foo=-1; else foo=1;
  204.                         if ((bar=b*x-c)<0) bar=-bar; 
  205.                         xx = y - foo*sqrt((double)bar);
  206.                         yy = a - x;
  207.                         x = xx;
  208.                         y = yy;
  209.                 }
  210.         }
  211.         do {
  212.                 ReplyMsg(msg);
  213.         } while (msg = (struct IntuiMessage *)GetMsg(win->UserPort));
  214.  
  215.  
  216.         finish:
  217.         CloseThings();
  218. }
  219.  
  220.